package tetris_invader.windows;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import tetris_invader.personagens.Fabrica;
import tetris_invader.util.Timer;
import tetris_invader.util.TimerListener;
import javazoom.jl.player.jlp;// localfile.mp3;

public class Principal extends JPanel implements TimerListener {

    protected Painel painel;
    private Frame frame;
    private JButton newGame;
    private JButton loadGame;
    private JButton options;
    private JButton help;
    private Image img[];
    private Timer timer;
    private jlp music;

    public Principal() throws JavaLayerException {
        Dimension d = new Dimension(150, 30);
        timer = new Timer(this, 100);
        music = openPlayer();

        painel = new Painel(new GridBagLayout());
        newGame = new JButton("New Game");
        loadGame = new JButton("Load Game");
        options = new JButton("Options");
        help = new JButton("Help");

        newGame.setPreferredSize(d);
        loadGame.setPreferredSize(d);
        options.setPreferredSize(d);
        help.setPreferredSize(d);
        painel.repaint();

        painel.setBackground(Color.black);
        painel.setName("Principal");

        painel.add(newGame, grid(1, 1, 1, 1));
        painel.add(loadGame, grid(1, 2, 1, 1));
        painel.add(options, grid(1, 3, 1, 1));
        painel.add(help, grid(1, 4, 1, 1));
    }

    public GridBagConstraints grid(int gridx, int gridy, int width, int height) {
        GridBagConstraints grid = new GridBagConstraints();

        grid.weightx = 10;
        grid.fill = GridBagConstraints.VERTICAL;
        grid.gridx = gridx;
        grid.gridy = gridy;
        grid.gridheight = height;
        grid.gridwidth = width;
        grid.anchor = GridBagConstraints.EAST;
        grid.insets = new Insets(10, 0, 10, 10);

        return grid;
    }

    public void start() {
        timer.start();
        try {
            music.play();
        } catch (JavaLayerException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Frame getFrame() {
        return frame;
    }

    public void setFrame(Frame frame) {
        this.frame = frame;
    }

    public void addListener(ActionListener ouvinte) {

        newGame.addActionListener(ouvinte);
        loadGame.addActionListener(ouvinte);
        options.addActionListener(ouvinte);
        help.addActionListener(ouvinte);
    }

    public void update() {
        repaint();
    }

    private jlp openPlayer() {
        jlp p = null;

        try {
            String filename = "src/sound/01.mp3";
            System.out.println("Nao");
            p = new jlp(filename);
            System.out.println("Teoricamente leu.");

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            System.out.println("Nao tá lendo");
        }
        
        return p;
    }

    public void playMusic(){
        try {
            music.play();
        } catch (JavaLayerException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void pause() {
        music = null;
        timer.pause();
    }

    public class Painel extends JPanel {

        public Painel(GridBagLayout g) {
            super(g);
        }

        public Painel() {
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawImage(backGround()[0], 0, 0, this);
        }

        public Image[] backGround() {
            Image[] img = new Image[1];

            String filename = "/images/background.png";
            URL url = Fabrica.class.getResource(filename);
            img[0] = Toolkit.getDefaultToolkit().getImage(url);

            return img;
        }
    }
}
